<html>
<head><meta charset="utf-8"><title>Collect ExactSizeIterator into an array? · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html">Collect ExactSizeIterator into an array?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="211440070"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211440070" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211440070">(Sep 27 2020 at 23:35)</a>:</h4>
<p>I know const-generics are coming along - is it possible to write this code?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="p">[</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">]</span>: <span class="p">[</span><span class="kt">i32</span><span class="p">;</span><span class="w"> </span><span class="mi">2</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="o">..</span><span class="mi">2</span><span class="p">).</span><span class="n">collect</span><span class="p">();</span><span class="w"></span>
</code></pre></div>

<p>It's not implemented on nightly, but is it possible in theory? <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=f4bea44ca888ece05a049416ae8acf06">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=f4bea44ca888ece05a049416ae8acf06</a></p>



<a name="211440298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211440298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211440298">(Sep 27 2020 at 23:40)</a>:</h4>
<p>I guess <code>size_hint()</code> is dynamic and not part of the type system so it would have to be fallible</p>



<a name="211443701"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211443701" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211443701">(Sep 28 2020 at 01:13)</a>:</h4>
<p>yeah, the error cases are a question -- if the iterator is too short, do you panic?</p>



<a name="211443709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211443709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211443709">(Sep 28 2020 at 01:13)</a>:</h4>
<p>if it's too long, do you leave those in the iterator? (that's what <code>ArrayVec</code> does)</p>



<a name="211449139"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211449139" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211449139">(Sep 28 2020 at 03:43)</a>:</h4>
<p>Iterators change size dynamically (fundamentally), so it's pretty much impossible to use the type system to do something infallible.</p>
<p>But yeah, I think the real answer is that we'll end up with <code>ArrayVec</code> in core.  We've got most of it already (between <code>[T; N]::map</code> and <code>core::array::IntoIter</code>), so the answer being to collect into an arrayvec and unwrap/try_into the arrayvec sounds like the right approach to me.</p>



<a name="211450297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211450297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jknodt <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211450297">(Sep 28 2020 at 04:13)</a>:</h4>
<p>I think <span class="user-mention" data-user-id="216206">@lcnr</span> is working on implementing something for this, since I'm not too up to date on what the exact plan is I'll just tag him</p>



<a name="211457623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211457623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211457623">(Sep 28 2020 at 07:00)</a>:</h4>
<blockquote>
<p>But yeah, I think the real answer is that we'll end up with ArrayVec in core. We've got most of it already (between [T; N]::map and core::array::IntoIter), so the answer being to collect into an arrayvec and unwrap/try_into the arrayvec sounds like the right approach to me.</p>
</blockquote>
<p><span aria-label="+1" class="emoji emoji-1f44d" role="img" title="+1">:+1:</span>  see discussion in <a href="#narrow/stream/219381-t-libs/topic/.60ArrayVec.60">https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/.60ArrayVec.60</a> and <a href="https://github.com/rust-lang/rust/issues/75717">#75717</a></p>



<a name="211457692"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211457692" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211457692">(Sep 28 2020 at 07:01)</a>:</h4>
<p>while I wouldn't mind someone else opening a PR for this, I will probably do so during the next few weeks once I have the time</p>



<a name="211488765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211488765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211488765">(Sep 28 2020 at 13:00)</a>:</h4>
<p><span class="user-mention" data-user-id="216206">@lcnr</span> I think you'll want to chime in on <a href="https://github.com/rust-lang/rfcs/pull/2990">https://github.com/rust-lang/rfcs/pull/2990</a></p>



<a name="211489008"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211489008" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211489008">(Sep 28 2020 at 13:03)</a>:</h4>
<p>I'm hoping for the syntax of</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">x</span>: <span class="nb">Result</span><span class="o">&lt;</span><span class="p">[</span><span class="n">N</span><span class="p">;</span><span class="w"> </span><span class="n">T</span><span class="p">],</span><span class="w"> </span><span class="n">LengthMismatchError</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">iter</span><span class="p">.</span><span class="n">collect</span><span class="p">();</span><span class="w"></span>
</code></pre></div>

<p>And <code>LengthMismatchError</code> is morally</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">enum</span> <span class="nc">LengthMismatchError</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">TooShort</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">contents</span>: <span class="nc">ArrayVec</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">N</span><span class="o">&gt;</span><span class="w"> </span><span class="p">},</span><span class="w"></span>
<span class="w">    </span><span class="n">TooLong</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">contents</span>: <span class="nc">ArrayVec</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">N</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">e</span>: <span class="nc">T</span><span class="p">,</span><span class="w"> </span><span class="n">i</span>: <span class="nb">Iterator</span> <span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="211499623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211499623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211499623">(Sep 28 2020 at 14:29)</a>:</h4>
<p>hmm, I personally think that it's better to pretend like collecting into arrays works like <code>.take(N).collect()</code>, so that we don't care about <code>TooLong</code>. But I guess <code>Result&lt;[T; N],  FillError&gt;</code> would make sense with <code>FillError&lt;T, N&gt;(ArrayVec&lt;T, N - 1&gt;)</code></p>



<a name="211499698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211499698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211499698">(Sep 28 2020 at 14:29)</a>:</h4>
<p>I think <code>Result&lt;[T; N], ArrayVec&lt;T; _&gt;&gt;</code> won't work because of coherence</p>



<a name="211502052"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211502052" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211502052">(Sep 28 2020 at 14:46)</a>:</h4>
<p>I'd disagree with your <code>take</code>-embedded version:</p>
<ol>
<li>You can always add <code>take(N)</code> yourself if that's what you want.</li>
<li>functions like <code>copy_from_slice</code> require exactly matching lengths as well.</li>
</ol>



<a name="211541936"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211541936" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211541936">(Sep 28 2020 at 19:56)</a>:</h4>
<p>An interesting thing about <code>TooLong</code> is that you can do it yourself with <code>.by_ref().collect()</code> and looking at the iterator afterwards.  But of course that has perf implications, and Jake's right about other things wanting exact matches in length (like `[T;N]: TryFrom&lt;&amp;[T]&gt;``).</p>



<a name="211542145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211542145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211542145">(Sep 28 2020 at 19:58)</a>:</h4>
<p>Honestly, I think I'd rather see length errors come from a <code>[T;N]: TryFrom&lt;ArrayVec&lt;T, N&gt;&gt;</code> that we'll want anyway, rather than adding more weird <code>Result</code> impls in <code>collect</code>.</p>



<a name="211542248"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211542248" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211542248">(Sep 28 2020 at 19:59)</a>:</h4>
<blockquote>
<p>But of course that has perf implications</p>
</blockquote>
<p>doing so explicitly has the same cost as <code>TooLong</code> but is opt in, or am I misunderstanding you here</p>



<a name="211542320"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211542320" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211542320">(Sep 28 2020 at 19:59)</a>:</h4>
<p>I mean that today <code>by_ref</code> iteration tends to inhibit optimizations in some cases.</p>



<a name="211542399"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211542399" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211542399">(Sep 28 2020 at 20:00)</a>:</h4>
<p><span aria-label="shock" class="emoji emoji-1f628" role="img" title="shock">:shock:</span> <a href="https://github.com/rust-lang/rust/issues/77307">#77307</a></p>



<a name="211542419"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211542419" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211542419">(Sep 28 2020 at 20:00)</a>:</h4>
<p>or because of something else?</p>



<a name="211542549"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211542549" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211542549">(Sep 28 2020 at 20:01)</a>:</h4>
<p>Because a lack of <code>Sized</code> specialization means that implementations of it can only use object-safe methods.</p>



<a name="211542663"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211542663" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211542663">(Sep 28 2020 at 20:02)</a>:</h4>
<p>I should try adding one again and see whether <code>min_specialization</code> can do it...</p>



<a name="211542798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211542798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211542798">(Sep 28 2020 at 20:03)</a>:</h4>
<p>so <code>by_ref</code> is</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">by_ref</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="bp">Self</span><span class="w"></span>
<span class="w">    </span><span class="k">where</span><span class="w"></span>
<span class="w">        </span><span class="bp">Self</span>: <span class="nb">Sized</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="bp">self</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>by default</p>



<a name="211542821"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211542821" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211542821">(Sep 28 2020 at 20:03)</a>:</h4>
<p>how do you want to specialize that method?</p>



<a name="211542889"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211542889" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211542889">(Sep 28 2020 at 20:03)</a>:</h4>
<p>(This is why <code>nth</code> doesn't use <code>try_fold</code>, for example)</p>



<a name="211543044"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211543044" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211543044">(Sep 28 2020 at 20:04)</a>:</h4>
<p>(Wait, why is that green?)</p>



<a name="211543162"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211543162" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211543162">(Sep 28 2020 at 20:05)</a>:</h4>
<p>what's green?</p>



<a name="211543600"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211543600" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211543600">(Sep 28 2020 at 20:08)</a>:</h4>
<p><a href="/user_uploads/4715/t4Z0p9-r-Z_U5oCEbUPZugx_/image.png">image.png</a></p>
<div class="message_inline_image"><a href="/user_uploads/4715/t4Z0p9-r-Z_U5oCEbUPZugx_/image.png" title="image.png"><img src="/user_uploads/4715/t4Z0p9-r-Z_U5oCEbUPZugx_/image.png"></a></div>



<a name="211544258"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211544258" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211544258">(Sep 28 2020 at 20:13)</a>:</h4>
<p>I think that's a highlight word configured for you</p>



<a name="211544279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211544279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211544279">(Sep 28 2020 at 20:13)</a>:</h4>
<p>Check <a href="#settings/alert-words">https://rust-lang.zulipchat.com/#settings/alert-words</a></p>



<a name="211544385"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211544385" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211544385">(Sep 28 2020 at 20:14)</a>:</h4>
<p>Oh, so it is.  I totally forgotten about that feature...</p>
<p>(Sorry for noise.)</p>



<a name="211546186"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211546186" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211546186">(Sep 28 2020 at 20:29)</a>:</h4>
<p>Well now I'm going to use that for my own amusement... try_fold</p>



<a name="211547715"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211547715" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211547715">(Sep 28 2020 at 20:43)</a>:</h4>
<p><span class="user-mention" data-user-id="216206">@lcnr</span> it's not the by_ref method that's the issue, it's the impl of Iterator for &amp;mut I where I: Iterator, which can't override many of the methods to delegate</p>



<a name="211547849"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211547849" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211547849">(Sep 28 2020 at 20:44)</a>:</h4>
<p>but <code>iter.by_ref().collect()</code> doesn't really have to override a method, does it?</p>



<a name="211551898"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211551898" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211551898">(Sep 28 2020 at 21:19)</a>:</h4>
<p>The <code>FromIterator</code> will call methods that might have been nice to be overloaded, though.  Not to mention other potential specialization -- <code>vec.into_iter().by_ref().collect()</code> as an obvious, if unlikely, one.</p>



<a name="211553220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211553220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211553220">(Sep 28 2020 at 21:33)</a>:</h4>
<p>I'm no longer following super well, but it may help to know that what I <em>actually</em> wanted was this, which didn't require going through iterators at all:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="c1">// split [a::c::d] into (a, c::d)</span>
<span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">iter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">path_str</span><span class="p">.</span><span class="n">rmatch_indices</span><span class="p">(</span><span class="s">"::"</span><span class="p">);</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">segments</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">iter</span><span class="p">.</span><span class="n">by_ref</span><span class="p">().</span><span class="n">take</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">idx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">segments</span><span class="p">.</span><span class="n">last</span><span class="p">()</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">base</span><span class="p">,</span><span class="w"> </span><span class="n">rest</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="o">&amp;</span><span class="n">path_str</span><span class="p">[</span><span class="o">..</span><span class="n">idx</span><span class="p">],</span><span class="w"> </span><span class="o">&amp;</span><span class="n">path_str</span><span class="p">[</span><span class="n">idx</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">2</span><span class="o">..</span><span class="p">]);</span><span class="w"></span>
</code></pre></div>



<a name="211553406"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211553406" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211553406">(Sep 28 2020 at 21:35)</a>:</h4>
<p>occasionally with some more error checking to make sure <code>iter.next().is_none()</code></p>



<a name="211567687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211567687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211567687">(Sep 29 2020 at 00:23)</a>:</h4>
<p>And FWIW, I don't think the valuable trait here is <code>ExactSizeIterator</code> but maybe <code>TrustedLen</code>.</p>



<a name="211567928"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211567928" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211567928">(Sep 29 2020 at 00:27)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F/near/211553220">said</a>:</p>
<blockquote>
<p>I'm no longer following super well, but it may help to know that what I <em>actually</em> wanted was this,</p>
</blockquote>
<p>This isn't valid code.</p>



<a name="211568090"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211568090" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211568090">(Sep 29 2020 at 00:29)</a>:</h4>
<p>But it looks like this does your intent:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">example</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nb">Option</span><span class="o">&lt;</span><span class="p">()</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">path_str</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"a::c::d"</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">iter</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">path_str</span><span class="p">.</span><span class="n">splitn</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="s">"::"</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">base</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">iter</span><span class="p">.</span><span class="n">next</span><span class="p">()</span><span class="o">?</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">rest</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">iter</span><span class="p">.</span><span class="n">next</span><span class="p">()</span><span class="o">?</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="n">dbg</span><span class="o">!</span><span class="p">(</span><span class="n">base</span><span class="p">,</span><span class="w"> </span><span class="n">rest</span><span class="p">);</span><span class="w"></span>

<span class="w">    </span><span class="nb">Some</span><span class="p">(())</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="211569081"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211569081" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211569081">(Sep 29 2020 at 00:47)</a>:</h4>
<p>no, that doesn't handle multiple segments in front properly: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=e443e92eb16d77801a4560085b26a4f4">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=e443e92eb16d77801a4560085b26a4f4</a></p>



<a name="211569185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211569185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211569185">(Sep 29 2020 at 00:49)</a>:</h4>
<p>oh I forgot a <code>.0</code>, here's the fixed verison: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=1a7f518d4b09500c144018e643f8988b">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=1a7f518d4b09500c144018e643f8988b</a></p>



<a name="211569265"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211569265" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211569265">(Sep 29 2020 at 00:50)</a>:</h4>
<blockquote>
<p>multiple segments in front</p>
</blockquote>
<p>Well, that's more of an issue with the testcases you provided.</p>



<a name="211569437"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211569437" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211569437">(Sep 29 2020 at 00:53)</a>:</h4>
<blockquote>
<p>which didn't require going through iterators at all:</p>
</blockquote>
<p>You also appear to be using a different definition of "didn't" or "iterator" than I am familiar with.</p>



<a name="211569523"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211569523" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211569523">(Sep 29 2020 at 00:54)</a>:</h4>
<p>lol, sorry I meant to say 'collect'</p>



<a name="211569557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211569557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211569557">(Sep 29 2020 at 00:55)</a>:</h4>
<p>the split() version means I can avoid <code>format!()</code> or <code>collect()</code>, so the lifetimes match up and the temporary isn't dropped</p>



<a name="211569682"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211569682" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211569682">(Sep 29 2020 at 00:57)</a>:</h4>
<p>for reference, this is how the code ended up: <a href="https://github.com/rust-lang/rust/blob/e414a998953c0cdf59b956322274cac5fe9dbf84/src/librustdoc/passes/collect_intra_doc_links.rs#L355">https://github.com/rust-lang/rust/blob/e414a998953c0cdf59b956322274cac5fe9dbf84/src/librustdoc/passes/collect_intra_doc_links.rs#L355</a></p>



<a name="211569695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211569695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211569695">(Sep 29 2020 at 00:57)</a>:</h4>
<p>... I should probably add some comments to that</p>



<a name="211569910"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211569910" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211569910">(Sep 29 2020 at 01:00)</a>:</h4>
<p>Clearly you want</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">SplitAround</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">rsplit_around</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;'</span><span class="na">a</span><span class="w"> </span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">needle</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="na">a</span> <span class="kt">str</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Box</span><span class="o">&lt;</span><span class="k">dyn</span><span class="w"> </span><span class="nb">Iterator</span><span class="o">&lt;</span><span class="n">Item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="o">&amp;</span><span class="kt">str</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="kt">str</span><span class="p">)</span><span class="o">&gt;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="o">'</span><span class="na">a</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">SplitAround</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="kt">str</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">rsplit_around</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;'</span><span class="na">a</span><span class="w"> </span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">needle</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="na">a</span> <span class="kt">str</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Box</span><span class="o">&lt;</span><span class="k">dyn</span><span class="w"> </span><span class="nb">Iterator</span><span class="o">&lt;</span><span class="n">Item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="o">&amp;</span><span class="kt">str</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="kt">str</span><span class="p">)</span><span class="o">&gt;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="o">'</span><span class="na">a</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Box</span>::<span class="n">new</span><span class="p">(</span><span class="bp">self</span><span class="p">.</span><span class="n">rmatch_indices</span><span class="p">(</span><span class="n">needle</span><span class="p">).</span><span class="n">map</span><span class="p">(</span><span class="k">move</span><span class="w"> </span><span class="o">|</span><span class="p">(</span><span class="n">idx</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">)</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">head</span><span class="p">,</span><span class="w"> </span><span class="n">tail</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">split_at</span><span class="p">(</span><span class="n">idx</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="n">tail</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">tail</span><span class="p">[</span><span class="n">needle</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="o">..</span><span class="p">];</span><span class="w"></span>
<span class="w">            </span><span class="p">(</span><span class="n">head</span><span class="p">,</span><span class="w"> </span><span class="n">tail</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">}))</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">correct</span><span class="p">(</span><span class="n">path_str</span>: <span class="kp">&amp;</span><span class="kt">str</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Option</span><span class="o">&lt;</span><span class="p">(</span><span class="o">&amp;</span><span class="kt">str</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="kt">str</span><span class="p">)</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">path_str</span><span class="p">.</span><span class="n">rsplit_around</span><span class="p">(</span><span class="s">"::"</span><span class="p">).</span><span class="n">nth</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="211570035"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211570035" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211570035">(Sep 29 2020 at 01:02)</a>:</h4>
<p>that looks useful, yeah</p>



<a name="211570044"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211570044" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211570044">(Sep 29 2020 at 01:02)</a>:</h4>
<p>could I convince you to add that to std? ;)</p>



<a name="211570083"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211570083" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211570083">(Sep 29 2020 at 01:03)</a>:</h4>
<p>if you made it less general (a function instead of a trait) I think you could get rid of the boxing</p>



<a name="211570448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211570448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211570448">(Sep 29 2020 at 01:08)</a>:</h4>
<p>You can get rid of the box with a custom iterator</p>



<a name="211570457"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211570457" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211570457">(Sep 29 2020 at 01:08)</a>:</h4>
<p>And if it were in the std, you could leverage the pattern API</p>



<a name="211570465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211570465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211570465">(Sep 29 2020 at 01:08)</a>:</h4>
<p>Oh for the day when the pattern API stabilizes.</p>



<a name="211693814"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211693814" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211693814">(Sep 29 2020 at 22:37)</a>:</h4>
<p>IIRC it has a lifetime in its trait -- is it one of those places that would really rather have GATs?</p>



<a name="211703698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211703698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211703698">(Sep 30 2020 at 00:53)</a>:</h4>
<p>Hmm. Are GATs baked enough that we could experiment with them for the Pattern API?</p>



<a name="211703901"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211703901" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211703901">(Sep 30 2020 at 00:57)</a>:</h4>
<p><span class="user-mention" data-user-id="125254">@kennytm</span> is my mentor for All Things Pattern</p>



<a name="211707469"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Collect%20ExactSizeIterator%20into%20an%20array%3F/near/211707469" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Collect.20ExactSizeIterator.20into.20an.20array.3F.html#211707469">(Sep 30 2020 at 02:02)</a>:</h4>
<p><a href="https://play.integer32.com/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=dd14a542bb97269888a1da2b791852fa">Seems present enough to play with such an API</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>